Lua Script Library
Lua Script Library Even a beginner programmer quickly learns the value of a library of common routines one can include in one's recipes. There are multiple ways of doing the same thing so if you have a way you think better just include it as an alternative. There's no point in arguing style. On the other hand, it's better to not to have multiple definitions for the very same named object. I, Gary Forbis, would like to propose the following guideline: Always define your functions and supporting global fields with at least two nodes. The first node is a table name idenifying a name space and the second either refines the name space or names the function. For example: math={} -- Return the absolute value of x function math.abs(x) if x < 0 then return -x else return x end end﻿ Also, include sample calls one can use to demonstrate the function: print(math.abs(-5)) -- will print 5 print(math.abs(5)) -- will also print 5 ﻿ Finally, reserve a namespace early. We can agree upon common library names. Where they already exist, such as math, please implement the semantics already identified for the members. Extensions to the standard libraries should be clearly identified as such. Well, that'smy introduction. Have fun and good scripting. 'Reserved Name Spaces' Reserve your name space here. base={} -- the lua standard library luaopen_base debug={} -- the lua standard library luaopen_debug gary={} -- Gary Forbis routines or modifications of others' routines math={} -- the lua standard library luaopen_math io={} -- the lua standard library luaopen_io package={} -- the lua standard library luaopen_package string={} -- the lua standard library luaopen_string table={} -- the lua standard library luaopen_table 'base -- the lua standard library luaopen_base' Except where identified these routines are in the standard lua library luaopen_base. Please be as faithful as possible to to their defintions. The implementation here differs from lua in that they are buried down one layer from global. In standard lua these functions would not be prefaced by base. 'base.assert -- test for false or nil' Issues an error when the value of its argument v is false (i.e., nil or false); otherwise, returns all its arguments. message is an error message; when absent, it defaults to "assertion failed!" function base.assert(v, message) if (v nil) or (v false) then if message nil print ("assertion failed!") else print(message) end assertion_failue=nil -- cause the script to fail after printing message assertion_failure() else return v end end I don't have access to any normal implementation of lua so am relyiing upon a best guess here. If lua returns on error then please correct. Use this routine when you want simple error checking without a bunch of fuss. -- example call to test band length in range base.assert((length >= 0 and length <= 20), "Band length out of range") 'math -- the lua standard library luaopen_math' Except where identified these routines are in the standard lua library luaopen_math. Please be as faithful as possible to their definitions. 'math.abs -- get the absolute value' Returns the absolute value of x. -- Return the absolute value of x function math.abs(x) if x < 0 then return -x else return x end end﻿ There's not much to say here. You should know when you want this function. print(math.abs(-5)) -- will print 5 print(math.abs(5)) -- will also print 5 ﻿ 'math.ceil -- get the smallest integer at or above a value' Returns the smallest integer larger than or equal to x -- Return the smallest integer at or above x function math.ceil(x) if x x%1 then return x else return x + 1 - (x%1) end end Again, there's not much to say here. print(math.ceil(-1)) -- will print -1 print(math.ceil(-.999)) -- will print 0 print(math.ceil(1.00001)) -- will print 2 'math.floor -- get the largest integer at or below a value' Returns the largest integer smaller than or equal to x. -- Return the smallest integer at or above x function math.floor(x) if x x%1 then return x else return x - (x%1) end end Again, there's not much to say here. print(math.ceil(-1)) -- will print -1 print(math.ceil(-.999)) -- will print -1 print(math.ceil(1.00001)) -- will print 1 'math.max -- get the largest of a set of numbers' Return the maximum value among its numeric arguments. function math.max(x,...) local max=x local args={...} for i=1,#args do if argsi>max then max=argsi end end return max end Sometimes you want to get the largest from a set of values. max is a good function for this when one isn't dealing with an array of values. print(math.max(1,4,2)) -- will print 4 'math.maxL -- get the largest number in a list' This is a non-standard function useful in foldit. It works like max except that it uses a table of numbers as a parameter. function math.maxL(x) local max=x1 for i=2,#x do if xi>max then max=xi end end return max end You can use this when you want to get the maximum value from a table of values. x=[1, 4, 2} print(math.maxL{(x)) -- will print 4 'math.maxT -- get the entry from a table of tables with the largest value at a given position' This is a non-standard function useful in foldit. It works like max except that it returns the table with the largest value in a particular position. The embedded table must have numeric indicies. function math.maxT(x,col) local maxC=x1col local maxI=1 for i=2,#x do if xicol>maxC then maxI=i maxC=xicol end end local maxT={} for i=1,#xmaxI do maxTi=xmaxIi end return maxT end This is a very useful routine when one wants to get the segment index with the best segment score: oldScores={} for x = 1,get_segment_count() do -- this doesn't work on ligand puzzles. oldScores#oldScores+1={x, get_segment_score(x)} end print('max at ',math.maxT(oldScores,2)1) 'math.min -- get the smallest of a set of numbers' Return the minimum value among its numeric arguments. function math.min(x,...) local min=x local args={...} for i=1,#args do if argsi